package com.ylx.web.p;

import com.ylx.pojo.Order;
import com.ylx.pojo.User;
import com.ylx.service.OrderService;
import com.ylx.service.OrderServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/accountServlet")
public class AccountServlet extends HttpServlet {
    protected void doGet (HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String oid = request.getParameter("oid");
        String password = request.getParameter("password");
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        OrderService orderService = new OrderServiceImpl();
        System.out.println(user.getPassword());
        System.out.println(password);
        if (password.equals(user.getPassword())) {

            orderService.updateOrderState(oid);
            response.sendRedirect("orderListServlet?currentPage=1");
        } else {

            request.setAttribute("error", "密码不正确，请确定后重新输入");
            Order order = orderService.getOrderByOid(oid);
            request.setAttribute("order", order);
            request.getRequestDispatcher("front/account.jsp").forward(request, response);
        }
    }


    protected void doPost (HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        this.doGet(request, response);

    }
}
